package com.zcz.authentication.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import com.zcz.authentication.service.ShiroService;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;


@Configuration
public class ShiroConfig {
	@Autowired
	private ShiroService shiroService;
//	@Autowired
//    private CacheManager cacheManager;
	
	/**
	 * 创建shirofilterfactorybean
	 */
	@Bean
	public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager securityManager) {
		System.err.println("创建shirofilterfactorybean拦截器");
		ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
		//设置安全管理器
		shiroFilterFactoryBean.setSecurityManager(securityManager);
		//添加shiro内置过滤器
		/**
		 * shiro内置过滤器，可以实现权限相关的拦截器
		 * 	常用的过滤器：
		 * 		anon:无需认证（登录）可以访问
		 * 		authc：必须认证才能访问
		 * 		user：如果使用rememberme 的功能可以直接访问
		 * 		perms：该资源必须得到资源权限才可以访问
		 * 		role：该资源必须得到角色权限才可以访问
		 */
		Map<String, String> filterMap = new LinkedHashMap<String, String>();
		// 定义过滤器名称 【注：map里面key值对于的value要为authc才能使用自定义的过滤器】
		Map<String, String> loadFilterChainDefinitionMap = shiroService.loadFilterChainDefinitionMap();
		filterMap.putAll(loadFilterChainDefinitionMap);
		//修改跳转的登录页面
		//shiroFilterFactoryBean.setLoginUrl("/toLogin");
		shiroFilterFactoryBean.setLoginUrl("/holle/toLogin");
		//设置未授权页面
		shiroFilterFactoryBean.setUnauthorizedUrl("/holle/noAuth");
		shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
		return shiroFilterFactoryBean;
	}
	/**
	 * 创建defauktwebsecuritymanager
	 */
	@Bean(name="securityManager")
	public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm")UserRealm userRealm
			) {
		System.err.println("创建defauktwebsecuritymanager");
		DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
		securityManager.setRealm(userRealm);
		//配置 ehcache缓存管理器
		EhCacheManager cacheManager = new EhCacheManager();
	    cacheManager.setCacheManagerConfigFile("classpath:ehcache-shiro.xml");
	    securityManager.setCacheManager(cacheManager);
		return securityManager;
	}
	/**
	 * 创建realm
	 */
	@Bean(name="userRealm")
	public UserRealm getRealm() {
		System.err.println(" 创建realm");
		UserRealm userRealm = new UserRealm();
		userRealm.setCachingEnabled(true);
		//启用身份验证缓存，即缓存AuthenticationInfo信息，默认false
		userRealm.setAuthenticationCachingEnabled(true);
	    //缓存AuthenticationInfo信息的缓存名称 在ehcache-shiro.xml中有对应缓存的配置
		userRealm.setAuthenticationCacheName("authenticationCache");
	    //启用授权缓存，即缓存AuthorizationInfo信息，默认false
		userRealm.setAuthorizationCachingEnabled(true);
	    //缓存AuthorizationInfo信息的缓存名称  在ehcache-shiro.xml中有对应缓存的配置
		userRealm.setAuthorizationCacheName("authorizationCache");
		return new UserRealm();
	}
	
	/**
	 * 配置shirodiakect ，用于thymeleaf和shiro标签配合使用
	 */
	@Bean
	public ShiroDialect getShiroDialect() {
		
		return new ShiroDialect();
	}
	
}
